<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      size="small"
    >
      <el-form-item label="选手" prop="playerName">
        <el-select
          v-model="queryParams.playerName"
          placeholder="请选择选手"
          clearable
          size="small"
        >
          <el-option
            v-for="option in playerOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        prop="id"
        label="ID"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="playerName"
        label="选手"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column label="介绍视频" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handlePlayVideo(scope.row)">
            播放视频
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        label="视频名称"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button type="text" @click="handleCopy(scope.row)"
            >复制地址</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <edit-introduce
      ref="editIntroduce"
      :visible.sync="editDialogVisible"
      :title="editDialogTitle"
      @success="handleEditSuccess"
    />

    <!-- 引入视频播放组件 -->
    <play-video
      :visible.sync="videoDialogVisible"
      :video-url="currentVideoUrl"
    />
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import EditIntroduce from "./components/edit.vue";
import { listPlayer, listPlayerIntroduce } from "@/api/player"; // 引入选手列表接口
import { getFile } from "@/utils/ali-oss"; // 添加这行引入
import PlayVideo from "./components/playVideo.vue";

export default {
  name: "PlayerIntroduce",
  components: {
    Pagination,
    EditIntroduce,
    PlayVideo,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        playerName: undefined,
      },
      tableData: [],
      editDialogVisible: false,
      editDialogTitle: "",
      playerOptions: [],
      videoDialogVisible: false,
      currentVideoUrl: "",
    };
  },
  created() {
    this.getPlayerOptions();
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const response = await listPlayerIntroduce(this.queryParams);
        // 处理视频URL
        this.tableData = await Promise.all(
          response.rows.map(async (row) => ({
            ...row,
            videoUrl: await getFile(row.content),
          }))
        );
        this.total = response.total;
      } catch (error) {
        this.$message.error("获取选手介绍列表失败");
        console.error("获取选手介绍列表失败", error);
      } finally {
        this.loading = false;
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.editDialogTitle = "新增选手介绍";
      this.editDialogVisible = true;
    },
    handleEdit(row) {
      this.editDialogTitle = "编辑选手介绍";
      this.editDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.editIntroduce.setFormData(row);
      });
    },
    handleDelete(row) {
      console.log("删除", row);
    },
    handleCopy(row) {
      const input = document.createElement("input");
      input.value = row.url;
      document.body.appendChild(input);
      input.select();
      document.execCommand("copy");
      document.body.removeChild(input);
      this.$message.success("地址已复制");
    },
    handleEditSuccess() {
      this.getList();
    },
    // 获取选手下拉列表数据
    getPlayerOptions() {
      listPlayer()
        .then((response) => {
          // 假设后端返回的数据格式为 { rows: [{id: 1, name: '选手名'}, ...] }
          this.playerOptions = response.rows.map((item) => ({
            label: item.name,
            value: item.id,
          }));
        })
        .catch((error) => {
          this.$message.error("获取选手列表失败");
          console.error("获取选手列表失败", error);
        });
    },
    // 添加视频播放处理方法
    handlePlayVideo(row) {
      this.currentVideoUrl = row.videoUrl;
      this.videoDialogVisible = true;
    },
    // 关闭视频对话框
    handleCloseVideo() {
      if (this.$refs.videoPlayer) {
        this.$refs.videoPlayer.pause();
      }
      this.videoDialogVisible = false;
      this.currentVideoUrl = "";
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.video-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}
</style>
